package com.jcl.recruit.verfity.logic;

import com.jcl.recruit.core.constants.Constants;
import com.jcl.recruit.verfity.model.VerificationCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @ClassName VerificationCodeLogic
 * @Description TODO
 * @Author yyliu
 * @Date 2018/9/18 9:37
 * @Version 1.0
 **/
@Component
public class VerificationValidateLogic {

    @Autowired
    private VerificationCodeLogic verificationCodeLogic;


    public int validatePhoneRegisterCode(String phone, String code){
        VerificationCode verificationCode = verificationCodeLogic.validateCode(phone,
                Constants.VerificationInfo.VERIFY_TYPE_PHONE_REGISTER, code);

        return validateCode(verificationCode);
    }


    public int validateEmailRegisterCode(String email, String code){
        VerificationCode verificationCode = verificationCodeLogic.validateCode(email,
                Constants.VerificationInfo.VERIFY_TYPE_EMAIL_REGISTER, code);

        return validateCode(verificationCode);
    }

    public int validateCode(VerificationCode verificationCode){
        if(null == verificationCode){
            // 验证码不正确
            return 1;
        }else{
            Date abateDate = verificationCode.getAbateDate();
            if(abateDate.before(new Date())){
                // 验证码失效
                return 2;
            }
        }
        return 0;
    }
}
